﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OptionFrm.aspx.cs" Inherits="WebApp.OptionFrm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>选项管理</title>
    <link rel="stylesheet" type="text/css" href="css/newStyle.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.3.2/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.3.2/themes/icon.css" />
    <style>
        .ftitle
        {
            border-bottom: 1px solid #CCCCCC;
            font-size: 14px;
            font-weight: bold;
            margin-bottom: 10px;
            padding: 5px 0;
        }
        .fitem
        {
            margin-bottom: 5px;
        }
    </style>

    <script type="text/javascript" src="js/common.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/jquery-1.8.0.min.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/jquery.easyui.min.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/locale/easyui-lang-zh_CN.js"></script>

    <script type="text/javascript" src="js/EasyUIExt.js"></script>

    <script type="text/javascript">
        var PageSize = 20;
        //页面加载
        $(window).load(function() {
            initData();
            initGrid();
        });

        $(window).resize(function() {
            $('#tb').datagrid('resize');
        });

        function doSearch() {
            var params = getParams();
            var url = "JSONHandler.ashx";

            $.post(url, params, function(data) {
                if (data)
                    $('#tb').datagrid('loadData', data);
            }, "json");
        }

        function getParams() {
            return {
                DataType: 'Options',
                OptionType: $('#qType').combobox('getValue')
            };
        }

        function initData() {
            $.getJSON("JSONHandler.ashx?DataType=OptionType", function(result) {
                $('#eType').combobox('loadData', result);
                $('#eType').combobox('selectedIndex', 0);
                $('#qType').combobox('loadData', result);
                $('#qType').combobox('selectedIndex', 0);
            });
        }

        //加载表格datagrid
        function initGrid() {
            //加载数据
            $('#tb').datagrid({
                //toolbar: '#searchBar',
                width: 'auto',
                fit: true,
                fitColumns: true,
                height: 'auto',
                striped: true,
                singleSelect: true,
                //url: 'JSONHandler.ashx',
                //queryParams: params,
                loadMsg: '数据加载中请稍后……',
                pagination: false,
                rownumbers: true,
                columns: [[
        { field: 'id', hidden: true },
        { field: 'type', title: '选项类型' },
        { field: 'name', title: '选项名称' }
    ]], onDblClickRow: function(rowIndex, rowData) {
        editOption();
    }
            });

        }

        var MEditId;
        function addOption() {
            $('#dlg').dialog('open').dialog('setTitle', '新增选项');
            $('#fm').form('clear');
            //$('#eType').combobox('selectedIndex', 0);
            $('#eType').combobox('setValue', $('#qType').combobox('getValue'));
            MEditId = 0;
        }

        function editOption() {
            var row = $('#tb').datagrid('getSelected');
            if (row) {
                $('#dlg').dialog('open').dialog('setTitle', '编辑选项');
                $('#fm').form('load', row);
                MEditId = row.id;
            }
        }

        function saveOption() {
            var validate = $('#fm').form('validate');
            if (!validate)
                return;
            var eType = $('#eType').combobox('getValue');
            var eName = $('#eName').val();
            WebApp.OptionFrm.SaveOption(MEditId, eType, eName, function callback(p) {
                if (p.error) {
                    $.messager.alert('异常消息', '提交保存异常,' + p.error.Message, 'error');
                    return;
                }

                var res = p.value;
                if (res) {
                    var data = res.Data;
                    var msg = res.Msg;
                    if (msg)
                        $.messager.alert('服务端异常信息', msg, 'error');
                    else {
                        if (MEditId > 0) {
                            if (data > 0) {
                                $.messager.alert('消息', '数据已更新!', 'info');
                                doSearch();
                            }
                            else
                                $.messager.alert('更新异常', msg, 'error');
                        }
                        else {
                            if (data > 0) {
                                $.messager.alert('消息', '数据已创建!', 'info');
                                MEditId = data;
                                doSearch();
                            }
                            else
                                $.messager.alert('创建异常', msg, 'error');
                        }
                    }
                }
            });
        }

        function delOption() {
            var row = $('#tb').datagrid('getSelected');
            if (row) {
                $.messager.confirm('提示', '确认删除该项吗?', function(r) {
                    if (r) {
                        WebApp.OptionFrm.DeleteOption(row.id, function callback(p) {

                            if (p.error) {
                                $.messager.alert('异常消息', '提交删除异常,' + p.error.Message, 'error');
                                return;
                            }
                            var res = p.value;
                            if (res) {
                                var data = res.Data;
                                var msg = res.Msg;
                                if (msg)
                                    $.messager.alert('删除异常', msg, 'error');
                                else {
                                    if (data == true) {
                                        $.messager.alert('消息', '数据已删除!', 'info');
                                        doSearch();
                                    }
                                    else
                                        $.messager.alert('删除失败', msg, 'error');
                                }
                            }
                        });
                    }
                });
            }
        }
    </script>

</head>
<body style="padding: 5px">
    <form id="form1" runat="server">
    </form>
    <div class="easyui-layout" fit="true">
        <div data-options="region:'north'" style="padding: 5px;">
            <span>选项类型：</span>
            <input class="easyui-combobox" id="qType" data-options="valueField:'Name',textField:'Name'"
                style="width: 100px;">
            <a href="#" class="easyui-linkbutton" iconcls="icon-search" plain="true" onclick="doSearch();">
                搜索</a>&nbsp; <a href="#" class="easyui-linkbutton" iconcls="icon-add" plain="true"
                    onclick="addOption();">新增</a>&nbsp;<a href="#" class="easyui-linkbutton" iconcls="icon-remove"
                        plain="true" onclick="delOption();">删除</a>
        </div>
        <div data-options="region:'center'" style="position: relative;">
            <table id="tb" class="easyui-datagrid">
            </table>
        </div>
    </div>
    <div id="dlg" class="easyui-dialog" style="width: 400px; height: 280px; padding: 10px 20px"
        closed="true" buttons="#dlg-buttons">
        <div class="ftitle">
            选项信息</div>
        <form id="fm" method="post">
        <div class="fitem">
            <label>
                选项类型:</label>
            <input class="easyui-combobox" name="type" id="eType" data-options="valueField:'Name',textField:'Name'"
                required="true" />
        </div>
        <div class="fitem">
            <label>
                选项名称:</label>
            <input name="name" id="eName" class="easyui-validatebox" required="true" />
        </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveOption()">
            保存</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel"
                onclick="javascript:$('#dlg').dialog('close')">关闭</a>
    </div>
    <div id="win">
        <iframe id="winFrame" src="" style="width: 0px; height: 0px" frameborder="0" scrolling="no">
        </iframe>
    </div>
</body>
</html>
